#include "talkmsglist.h"
#include <QDebug>
#include <QAction>
#include <QIcon>
#include <QHostAddress>
#include "contactlist.h"
#include "talkmsglistitem.h"
TalkMsgList::TalkMsgList(QListWidget *parent) :  QListWidget(parent)
{
    setFocusPolicy(Qt::NoFocus);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void TalkMsgList::mousePressEvent(QMouseEvent *event)
{
    QListWidget::mousePressEvent(event);
    currentItem = this->itemAt(mapFromGlobal(QCursor::pos()));
    if(event->button()==Qt::LeftButton&&currentItem!=NULL){
        QWidget* pwig = this->itemWidget(currentItem);
        QList<ContactInfo*> contactInfoList = pwig->findChildren<ContactInfo*>();
        for (int i=0;i<contactInfoList.size();i++) {
            qDebug()<<contactInfoList[0];
            emit contactItemClick(contactInfoList[0]);
        }
    }


}
void TalkMsgList::slotAddMsg(ContactInfo *contactInfo,bool isMe)
{

    TalkMsgListItem *msgItem=new TalkMsgListItem(isMe);
    msgItem->avatarPath=contactInfo->avatarPath;
    msgItem->nickName->setText(contactInfo->sourceHostUserName);
    msgItem->hostName->setText(contactInfo->msgPayLoad);
    contactInfo->setParent(msgItem);
    msgItem->contactInfo=contactInfo;
    int textContHeight=msgItem->hostName->sizeHint().height();
    msgItem->hostName->setMinimumWidth(msgItem->hostName->sizeHint().width());
    msgItem->hostName->setMaximumWidth(msgItem->hostName->sizeHint().width());
    QListWidgetItem *newItem = new QListWidgetItem();
    QSize itemSize=QSize();
    itemSize.setHeight(40+textContHeight);

    newItem->setSizeHint(itemSize);
    this->addItem(newItem);
    this->setItemWidget(newItem, msgItem);
    this->scrollToBottom();

}


